﻿using System.Linq.Expressions;
using DataManageSystem.Common;
using DataManageSystem.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace DataManageSystem.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class EquipmentController : ControllerBase
    {
        private readonly EquipmentServices _equipmentService;

        public EquipmentController(EquipmentServices equipmentService)
        {
            _equipmentService = equipmentService;
        }

        [HttpGet("selectByPage")]
        public ObjectResult selectByPage(int pageNum = 1, int pageSize = 10, string? name = "")
        {
            var res = _equipmentService.SelectByPage(pageNum, pageSize, name);
            return new ObjectResult(res);
        }

        [HttpGet("importDevicesByDateBase")]
        public ObjectResult importDevicesByDateBase()
        {
            try
            {
                _equipmentService.ImportDevicesByDateBase();
                return new ObjectResult(Result.Success("设备导入成功"));
            }
            catch (Exception ex)
            {
                return new ObjectResult(Result.Error(ex.Message));
            }
        }

        [HttpGet("importDataPoint")]
        public ObjectResult importDataPoint(string registerCode)
        {
            try
            {
                _equipmentService.ImportDataPoint(registerCode);
                return new ObjectResult(Result.Success("数据点位导入成功"));
            }
            catch (Exception ex)
            {
                return new ObjectResult(Result.Error(ex.Message));
            }
        }

        [HttpGet("getDataCount")]
        public ObjectResult getDataCount()
        {
            var total = _equipmentService.GetDataCount();
            var running = _equipmentService.GetDataCount();
            var fault = _equipmentService.GetDataCount();

            var result = new List<object>
            {
                new { key = "接入设备数", value = total },
                new { key = "运行设备数", value = running },
                new { key = "故障设备数", value = fault }
            };
            return new ObjectResult(Result.Success(result));
        }


        [HttpPost("import")]
        public IActionResult ImportEquipments([FromQuery] string equipmentCode)
        {
            _equipmentService.OETableImport(equipmentCode);
            return Ok("数据导入成功");
        }

        [HttpPost("import2")]
        public IActionResult ImportEquipment2([FromQuery] string equipmentCode)
        {
            _equipmentService.OETableImport2(equipmentCode);
            return Ok("数据同步成功");
        }

        [HttpPost("import3")]
        public IActionResult ImportEquipment3([FromQuery] string equipmentCode)
        {
            _equipmentService.OETableImport3(equipmentCode);
            return Ok("数据同步成功");
        }

        [HttpPost("import4")]
        public IActionResult ImportEquipment4([FromQuery] string equipmentCode)
        {
            _equipmentService.OETableImport4(equipmentCode);
            return Ok("数据同步成功");
        }

        [HttpPost("uploadEx")]
        public ObjectResult GenRegister(IFormFile file)
        {
            try
            {
                _equipmentService.imortFromExcel(file);
                return Ok(new { success = true});
            }
            catch (Exception ex)
            {
                return BadRequest(new { success = false, error = ex.Message });
            }


        }


    }

}

